936C - Lock Puzzle - CodeForces Solution


constructive algorithms implementation strings *2300

Please click on ads to support us..

Python Code:

n = int(input())
src, tar = input(), input()
ans = []

def shift(s, x):
    ans.append(x)
    return s[-x:][::-1] + s[:-x]

flag = True
for i in range(n):
    index = -1
    for j in range(n-i):
        if src[j] == tar[i]:
                        index = j
            break
    if index == -1:
        flag = False
        break
    if index == n-1:
        continue
    src = shift(src, n - index - 1)
    src = shift(src, 1)
    src = shift(src, n)
    
if flag:
    ans = [str(x) for x in ans]
    print(len(ans))
    print(' '.join(ans))
else:
    print(-1)


Comments

Submit
0 Comments
More Questions

22B - Bargaining Table
1490B - Balanced Remainders
264A - Escape from Stones
1506A - Strange Table
456A - Laptops
855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push
706B - Interesting drink
1265A - Beautiful String
214A - System of Equations
287A - IQ Test
1108A - Two distinct points
1064A - Make a triangle
1245C - Constanze's Machine